package com.web.exiaodao.business.controller;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.web.webstart.base.constant.XaConstant;
import com.web.webstart.base.controller.BaseController;
import com.web.webstart.base.exception.BusinessException;
import com.web.webstart.base.util.jpa.WebUitl;
import com.web.webstart.base.util.result.XaResult;
import com.web.exiaodao.business.constant.JConstant;
import com.web.exiaodao.business.entity.ExperienceTicketRecord;
import com.web.exiaodao.business.service.ExperienceTicketRecordService;
import com.web.exiaodao.remote.vo.ExperienceTicketRecordVo;

/**
 * @Title: ExperienceTicketRecordController.java
 * @Package com.web.webstart.business.controller
 * @Description: 体验券领取记录控制器
 * @author wxm
 * @date 2016-12-28 10:05:04
 * @version V1.0
 */
@Controller
@RequestMapping("/cms/experienceTicketRecord")
public class ExperienceTicketRecordController extends BaseController {

	private static final Logger LOGGER = Logger.getLogger(ExperienceTicketRecordController.class);

	@Autowired
	private ExperienceTicketRecordService experienceTicketRecordService;

	/**
	 * @Title: findExperienceTicketRecordNEStatusPage
	 * @Description: 分页查询ExperienceTicketRecord	 * @param nextPage
	 * @param pageSize
	 * @param status		//默认查询非删除3的数据,具体类型参照XaConstant.Status
	 * @param sortData
	 * @param jsonFilter
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="findExperienceTicketRecordNEStatusPage",method=RequestMethod.POST)
	public XaResult<Page<ExperienceTicketRecordVo>> findListNEStatus(
		@RequestParam(defaultValue = "0") Integer nextPage,
		@RequestParam(defaultValue = "10") Integer pageSize,
		@RequestParam(defaultValue = "2") Integer status,
		@RequestParam(defaultValue = "[{property:'createTime',direction:'DESC'}]") String sortData,
		//过滤字段,字段名:jsonFilter,选填,默认:{},示例:{'search_EQ_field1':'value1','search_EQ_field2':'value2'},字段名称拼接规则search_为固定查询标识,EQ为等于,filed为字段名
		//EQ等于, IN包含, ISNULL空, LIKE, GT大于, LT小于, GTE大于等于, LTE小于等于, NE不等于,LIKEIGNORE非like
		@RequestParam(defaultValue = "{}") String jsonFilter
	) throws BusinessException{
		XaResult<Page<ExperienceTicketRecordVo>> xr = new XaResult<Page<ExperienceTicketRecordVo>>();
		try{
			Pageable pageable = WebUitl.buildPageRequest(nextPage, pageSize, sortData);
			Map<String,Object> filterParams =  WebUitl.getParametersStartingWith(jsonFilter, "search_");
			return experienceTicketRecordService.findListNEStatus(status, filterParams, pageable);
		}catch(Exception e){
			if (e instanceof BusinessException) {
				xr.error(e.getMessage());
			} else {
				xr.error(XaConstant.Message.error);
			}
			LOGGER.error(XaConstant.Message.error,new Throwable(e));
		}
		return xr;
	}

	/**
	 * @Title: findExperienceTicketRecordEQStatusPage
	 * @Description: (预留)分页查询ExperienceTicketRecord	 * @param nextPage
	 * @param pageSize
	 * @param status		//默认查询正常状态1的数据,具体类型参照XaConstant.Status
	 * @param sortData
	 * @param jsonFilter
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="findExperienceTicketRecordEQStatusPage",method=RequestMethod.POST)
	public XaResult<Page<ExperienceTicketRecord>> findExperienceTicketRecordEQStatusPage(
		@RequestParam(defaultValue = "0") Integer nextPage,
		@RequestParam(defaultValue = "10") Integer pageSize,
		@RequestParam(defaultValue = "1") Integer status,
		@RequestParam(defaultValue = "[{property:'createTime',direction:'DESC'}]") String sortData,
		@RequestParam(defaultValue = "{}") String jsonFilter
	) throws BusinessException{
		XaResult<Page<ExperienceTicketRecord>> xr = new XaResult<Page<ExperienceTicketRecord>>();
		try{
			Pageable pageable = WebUitl.buildPageRequest(nextPage, pageSize, sortData);
			Map<String,Object> filterParams =  WebUitl.getParametersStartingWith(jsonFilter, "search_");
			return experienceTicketRecordService.findListEQStatusByFilter(status, filterParams, pageable);
		}catch(Exception e){
		if (e instanceof BusinessException) {
			xr.error(e.getMessage());
		} else {
			xr.error(XaConstant.Message.error);
		}
			LOGGER.error(XaConstant.Message.error,new Throwable(e));
		}
		return xr;
	}

	/**
	 * @Title: findExperienceTicketRecordById
	 * @Description: 根据ID查找单条实体
	 * @param modelId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="findExperienceTicketRecordById",method=RequestMethod.POST)
	public XaResult<ExperienceTicketRecord> findExperienceTicketRecordById(
		@RequestParam Long modelId
	) throws BusinessException{
		XaResult<ExperienceTicketRecord> xr = new XaResult<ExperienceTicketRecord>();
		try{
			return experienceTicketRecordService.findOne(modelId);
		}catch(Exception e){
		if (e instanceof BusinessException) {
			xr.error(e.getMessage());
		} else {
			xr.error(XaConstant.Message.error);
		}
			LOGGER.error(XaConstant.Message.error,new Throwable(e));
		}
		return xr;
	}
	/**
	 * @Title: saveModel
	 * @Description: 保存实体数据
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="saveExperienceTicketRecord",method=RequestMethod.POST)
	public XaResult<ExperienceTicketRecord> saveExperienceTicketRecord(
		ExperienceTicketRecord model
	) throws BusinessException{
		XaResult<ExperienceTicketRecord> xr = new XaResult<ExperienceTicketRecord>();
		try{
			return experienceTicketRecordService.saveOrUpdate(model);
		}catch(Exception e){
		if (e instanceof BusinessException) {
			xr.error(e.getMessage());
		} else {
			xr.error(XaConstant.Message.error);
		}
			LOGGER.error(XaConstant.Message.error,new Throwable(e));
		}
		return xr;
	}

	/**
	 * @Title: multiOperateExperienceTicketRecordByIds
	 * @Description: 批量操作多个实体状态,根据status进行操作
	 * @param modelIds    	多个实体id,中间用逗号隔开
	 * @param status 		操作类型,status类型见XaConstant.Status,默认删除操作
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="operateExperienceTicketRecordByIds",method=RequestMethod.POST)
	public XaResult<ExperienceTicketRecord> operateExperienceTicketRecordByIds(
		HttpServletRequest request,
		@RequestParam String modelIds,
		@RequestParam(defaultValue = "3") Integer status
	) throws BusinessException{
		XaResult<ExperienceTicketRecord> xr = new XaResult<ExperienceTicketRecord>();
		try{
			return experienceTicketRecordService.multiOperate(modelIds,status);
		}catch(Exception e){
		if (e instanceof BusinessException) {
			xr.error(e.getMessage());
		} else {
			xr.error(XaConstant.Message.error);
		}
			LOGGER.error(XaConstant.Message.error,new Throwable(e));
		}
		return xr;
	}
	
	 /**
     * @Title: upload
     * @Description: 导出文件到 office xls
     * @return
     * @throws BusinessException
     */
    @ResponseBody
    @RequestMapping(value = "fileDownload", method = RequestMethod.GET)
    public void fileDownload(
            @RequestParam(defaultValue = "{}") String jsonFilter,
            @RequestParam(required=false) String modelIds,
            HttpServletResponse response) throws BusinessException {
        try {
            jsonFilter = URLDecoder.decode(jsonFilter, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Map<String, Object> filterParams = WebUitl.getParametersStartingWith("{'search_IN_id':'" +
                JSON.toJSONString(modelIds.split(JConstant.SYMBOL_DOT)) + "'}", "search_");
        experienceTicketRecordService.exportdata(filterParams, response,modelIds);
    }

}

